<?php

//短信息模型
class Message_model extends Model {

	function Message_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("accounts_model", "am");
	}
	
	//获取未读收件信息
	function new_messages($userid)
	{
		$query = $this->db->get_where("message_inbox", array("toid"=>$userid, "isread"=>0));
		return $query->num_rows();
	}
	
	//获取通知信息
	function new_notices($userid)
	{
		$query = $this->db->get_where("message_notice", array("userid"=>$userid, "isread"=>'0'));
		return $query->num_rows();
	}
	
	//发送通知
	function send_notice($userid, $title)
	{
		//发件箱
		$data = array(
			"userid" => $userid,
			"title" => $title,
			"addtime" => time(),
		);
		$this->db->insert("message_notice", $data);
		$result = $this->db->insert_id();
		return $result;
	}
	
	//获取通知信息
	function lists_notice($userid, $limit = 2, $offset = 1)
	{
		$lists = array();
		$query = $this->db->from("message_notice")->where("userid",$userid)->limit($limit, $offset)->order_by("id","desc")->get();
		if ($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $list) {
				$lists[$k]["showtitle"] = strip_tags($list["title"]);
			}
		}
		return $lists;
	}
	
	//获取通知信息总数
	function count_list_notice($userid)
	{
		$query = $this->db->get_where("message_notice", array("userid"=>$userid));
		return $query->num_rows();
	}
	
	//设置已读
	function setread_notice($userid)
	{
		$data = array(
			"isread" => 1,
		);
		$this->db->where("userid", $userid);
		$result = $this->db->update("message_notice", $data);
		return $result;
	}
	
	//设置已读
	function setread($messageid, $box = "inbox")
	{
		$data = array(
			"isread" => 1,
		);
		$this->db->where("id", $messageid);
		$result = $this->db->update("message_".$box, $data);
		return $result;
	}
	
	//删除所有
	function deleteall($userid, $box = "inbox")
	{
		if(!in_array($box, array("inbox","outbox", "notice"))) {
			$box = "inbox";
			$userfield = "toid";
		} elseif($box == "outbox") {
			$userfield = "fromid";
		} elseif ($box == "notice") {
			$userfield = "userid";
		}
		
		$this->db->where($userfield, $userid);
		$result = $this->db->delete("message_".$box);
		return $result;
	}
	
	//删除信息
	function delete($messageid, $box = "inbox")
	{
		$this->db->where("id", $messageid);
		$result = $this->db->delete("message_".$box);
		return $result;
	}
	
	//删除选择信息
	function delselected($delstr, $box = "inbox")
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		$this->db->where_in("id", $delid);
		$result = $this->db->delete("message_".$box);
		return $result;
	}
	
	//查看信息
	function show($userid, $messageid, $box = "inbox")
	{	
		$row = array();
		if($box != "outbox") {
			$box = "inbox";
			$userfield = "toid";
			$userfield2 = "fromid";
			$mtitle = "收信";
			$mstr = "来自";
		} else {
			$userfield = "fromid";
			$userfield2 = "toid";
			$mtitle = "发信";
			$mstr = "发往";
		}
		$query = $this->db->get_where("message_".$box, array("id"=>$messageid, $userfield=>$userid));
		if ($query->num_rows()>0) {
			$row = $query->row_array();
			if($row[$userfield2] > 0){
				$user = $this->am->get($row[$userfield2]);
				$username = $user["username"];
			} else {
				$username = "热闹网";
			}
			$row["muser"] = $username;
			$row["muserid"] = $row[$userfield2];
			$row["mtitle"] = $mtitle;
			$row["mstr"] = $mstr;
			$row["box"] = $box;
		}
		return $row;
	}
	
	//发送信息
	function send($fromid, $toid, $title, $description)
	{
		//发件箱
		$data = array(
			"fromid"  => $fromid,
			"toid" => $toid,
			"title" => $title,
			"description" => $description,
			"addtime" => time(),
		);
		$this->db->insert("message_outbox", $data);
		$result = $this->db->insert_id();
		
		//收件箱
		$this->db->insert("message_inbox", $data);
		$result = $this->db->insert_id();
		return $result;
	}
	
	//获取发件箱信息
	function lists_outbox($fromid, $limit = 2, $offset = 1)
	{
		$lists = array();
		$query = $this->db->from("message_outbox")->where("fromid",$fromid)->limit($limit, $offset)->order_by("id","desc")->get();
		if ($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $row){
				$toid = $row["toid"];
				$touser = $this->am->get($toid);
				$lists[$k]["touser"] = $touser["username"];
			}
		}
		return $lists;
	}
	
	//获取发件箱信息总数
	function count_list_outbox($fromid)
	{
		$query = $this->db->get_where("message_outbox", array("fromid"=>$fromid));
		return $query->num_rows();
	}
	
	//系统发送信息
	function sys_send($toid, $title, $description)
	{
		//发件箱
		$data = array(
			"fromid"  => 0,
			"toid" => $toid,
			"title" => $title,
			"description" => $description,
			"addtime" => time(),
		);
		//收件箱
		$this->db->insert("message_inbox", $data);
		$result = $this->db->insert_id();
		return $result;
	}
	
	//获取收件箱所有信息
	function listitem($toid = 0, $limit = 2, $offset = 1)
	{
		$lists = array();
		$this->db->from("message_inbox");
		if($toid > 0) {
			$this->db->where("toid",$toid);
		}
		$query = $this->db->limit($limit, $offset)->order_by("id","desc")->get();
		if ($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $row){
				$fromid = $row["fromid"];
				$toid = $row["toid"];
				if($fromid > 0) {
					$fromuser = $this->am->get($fromid);
					$fromusername = $fromuser["username"];
				} else {
					$fromusername = '热闹网';
				}
				$touser = $this->am->get($toid);
				$lists[$k]["fromuser"] = $fromusername;
				$lists[$k]["touser"] = $touser["username"];
			}
		}
		return $lists;
	}
	
	//计算收件箱所有总数
	function countitems()
	{
		return $this->db->count_all_results("message_inbox");
	}
	
	//获取收件箱信息
	function lists_inbox($toid, $limit = 2, $offset = 1)
	{
		$lists = array();
		$query = $this->db->from("message_inbox")->where("toid",$toid)->limit($limit, $offset)->order_by("id","desc")->get();
		if ($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $row){
				$fromid = $row["fromid"];
				if($fromid > 0) {
					$fromuser = $this->am->get($fromid);
					$fromusername = $fromuser["username"];
				} else {
					$fromusername = '热闹网';
				}
				$lists[$k]["fromuser"] = $fromusername;
			}
		}
		return $lists;
	}
	
	//获取收件箱信息总数
	function count_list_inbox($toid)
	{
		$query = $this->db->get_where("message_inbox", array("toid"=>$toid));
		return $query->num_rows();
	}
	
}

/* End of file */